package com.example.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class usernameInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        if (requestURI.startsWith("/sec/")) {
            HttpSession session = request.getSession();
            Object obj = session.getAttribute("login_user");
            if (obj == null) {
                response.sendRedirect("/login_input");
                return false;
            }
            String username = obj.toString();
            if (!username.endsWith("r")) {
                response.sendRedirect("/login_input");
                return false;
            }
        }
        return true;
    }
}
